import {
  ConfirmModel
} from "../../models/confirm";
import {
  PayModel
} from '../../models/pay'
import {
  Format
} from '../../utils/common'
import {
  Common
}
from "../../utils/common";
import {
  AccountModel
} from '../../models/account'
const accountmodel = new AccountModel()
const app = getApp();
const confirmModel = new ConfirmModel()
const payModel = new PayModel()
const format = new Format()
const common = new Common()
Page({
    data: {
        btnDisabled: true,
        openPawPop: false,
        gettruePaw: '',
        radioitems: [
          {name: '支付宝', value: 1, checked: true, icon: '/assets/payIcon2.jpg'},
          {name: '壳牌会员卡', value: 3, icon: '/assets/payIcon3.jpg'}
        ]
    },
    onLoad(options) {
        this.setData({
            'payItem': JSON.parse(options.item),
            // 'payItem.tradeType': 1,
        })
        console.log(this.data.payItem.oilNum)
        console.log(this.data.payItem)
    },
    onShow: function (options) {
    },
    radioChange: function (e) {
        console.log(e)
        this.setData({
            'payItem[tradeType]': e.detail.value
        })
    },
    toPay() {
        const d = this.data.payItem
        // this.createTradeNew('')
        if (d.tradeType == 1 || d.tradeType == 6) {
          this.createTradeNew('')
        } else {
          if (app.globalData.ifSetPaw == 0) {
            this.setData({
                modalOpened4: true,
            });
          } else if (app.globalData.ifSetPaw == 1) {
            this.setData({
              openPawPop: true
            })
          } else {
            this.checkOriginPwd()
          }
        }
        // if (d.tradeType == 1) {
          // payModel.createTradeNew(d.tradeType,d.amount, d.realAmount, d.subject, d.shopId, d.jiayouSource,d.orderId,d.goodsId,d.oilGunId).then(res => {
          //   console.log(res)
          //   if (res && res.orderId) {
          //       if (d.tradeType == 1) {
          //         payModel.myPay('consume',res.tradeNo, d.realAmount, d.canUseTicketAmount, d.gunNum, d.oilNum)
          //       } else {
          //         wx.redirectTo({
          //           url: '/pages/finish/finish?tradeNo=' + res.orderId
          //         })
          //       }
          //   } else {
          //     if (res.message) {
          //       console.log(res.message)
          //       wx.showToast({
          //         type: 'none',
          //         content: res.message,
          //         duration: 600,
          //       });
          //     } else {
          //       console.log('11111')
          //       wx.showToast({
          //         type: 'none',
          //         content: '交易失败，请重试',
          //         duration: 600,
          //       });
          //     }
          //   }
          // });
        // } else {
          // if (app.globalData.ifSetPaw) {
          //   this.setData({
          //     openPawPop: true
          //   })
          // } else {
          //   this.checkOriginPwd()
          // }
        //   
        //   // wx.redirectTo({
        //   //   url: '/pages/finish/finish?tradeNo=' + res.orderId
        //   // })
        // }
    },
    createTradeNew(payPassword) {
      const d = this.data.payItem
      // 花呗分期
      let periodsNumber = ''
      let serviceCharge = ''
      let periodCost = ''
      if (d.tradeType == 6 && d.selectHuabei) {
        periodsNumber = (d.selectHuabei.number || '')
        serviceCharge = (d.selectHuabei.serviceCharge || '')
        periodCost = (d.selectHuabei.periodCost || '')
      }
      payModel.createTradeNew(payPassword,d.tradeType,d.amount, d.realAmount, d.subject, d.shopId, d.jiayouSource,d.orderId,d.goodsId,d.oilGunId,periodsNumber,periodCost,serviceCharge).then(res => {
          console.log(res)
        if (res && res.orderId) {
            if (d.tradeType == 1  || d.tradeType == 6) {
              payModel.myPay('consume',res.tradeNo, d.realAmount, d.canUseTicketAmount, d.gunNum, d.oilNum)
            } else {
              this.setData({
                openPawPop: false
              })
              wx.redirectTo({
                url: '/pages/finish/finish?tradeNo=' + res.orderId
              })
            }
        } else {
          if (res && res.message) {
            console.log(res.message)
            wx.showToast({
              type: 'none',
              content: res.message,
              duration: 600,
            });
          } else {
            console.log('11111')
            wx.showToast({
              type: 'none',
              content: '交易失败，请重试',
              duration: 600,
            });
          }
        }
      });
    },
    checkOriginPwd() {
      accountmodel.checkOriginPwd().then(res => {
          console.log(res)
          if (res && res.code == '00') {
            if (res.pinStatus == 0) {
              app.globalData.ifSetPaw = 0
              this.setData({
                modalOpened4: true,
              });
            } else {
              app.globalData.ifSetPaw = 1
              this.setData({
                openPawPop: true
              })
            }
          } else {
            wx.showToast({
              type: 'none',
              content: '接口错误',
              duration: 1000,
            });
          }
        })
    },
    onModalClick21() {
      this.setData({
        modalOpened4: false,
      });
      wx.navigateTo({
        url: '/pages/account/setPaw/setPaw'
      })
    },
    onModalClose21() {
      this.setData({
        modalOpened4: false,
      });
    },
    gocardPay(paw) {
      this.createTradeNew(paw)
      console.log(paw + '~~~~~~~~~~~~')
    },
    closePawPop() {
      this.setData({
        openPawPop: false
      })
    },
});